﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_DLL.Product.database;
using ERP_RORZE_DLL.Product.DAO;
using ERP_RORZE_DLL.Product.Base;
using ERP_RORZE_DLL.Product;
using ERP_RORZE_PERMISSION.Base;
using BaseDAO = ERP_RORZE_TOOL.DBAccess.BaseDAO;
using Utilities = ERP_RORZE_TOOL.Common.Utilities;

namespace ERP_RORZE_PRODUCT.Product.PartData
{
    public partial class Frm_OptionDataEdit : DevExpress.XtraEditors.XtraForm
    {
        ClsOptionDataTable obj = new ClsOptionDataTable();
        ClsPartDataDAO clspartDAO = new ClsPartDataDAO();
        DataTable dt;
        public EventHandler Re_Load;
        private string minDate = "1753-01-01 00:00:00.000";
        public string ID;
        public string InputDate;
        public string PartID;
        public string MachineID;
        private LogUtils log;
        public string OptionID;
        public string LastOption;
        public string JigID;
        public string ToolQty;
        public string ProTime;
        public string ClampTime;
        public string TechDate;
        public string UpdateDay;
        public string StaffID;
        public string Note;
        public string AondNote;
        public string Memo;
        public string CLUpdateday;
        public string Jig;
        public string JigType;

        public Frm_OptionDataEdit()
        {
            InitializeComponent();
            log = new LogUtils();
        }

        private void Frm_OptionDataEdit_Load(object sender, EventArgs e)
        {
            //lấy PartID  PartID

            dt = BaseDAO.ExecuteQuery("select [242_PartData].PartID from [242_PartData]");
            DataRow r1 = dt.NewRow();
            r1["PartID"] = "";
            dt.Rows.InsertAt(r1, 0);
            cbPartID.DataSource = dt;
            cbPartID.DisplayMember = "PartID";
            cbPartID.ValueMember = "PartID";

            // lấy StaffID
            DataTable dt1;
            dt1 = clspartDAO.GetAllStaffID();
            DataRow r = dt1.NewRow();
            r["FullName"] = "";
            r["StaffID"] = "";
            dt1.Rows.InsertAt(r, 0);
            cbStaffID.DataSource = dt1;
            cbStaffID.DisplayMember = "FullName";
            cbStaffID.ValueMember = "StaffID";

            // lấy OptionID
            DataTable dt2;
            dt2 = clspartDAO.GetAllOptionID();
            DataRow r2 = dt2.NewRow();
            r2["OptionID"] = "";
            r2["OptionID"] = "";
            dt2.Rows.InsertAt(r2, 0);
            cbOptionID.DataSource = dt2;
            cbOptionID.DisplayMember = "OptionID";
            cbOptionID.ValueMember = "OptionID";

            //
            DataTable dt3;
            dt3 = clspartDAO.GetAllMachineID();
            DataRow r3 = dt3.NewRow();
            r3["MachineID"] = "";
            r3["MachineID"] = "";
            dt3.Rows.InsertAt(r3, 0);
            cbMachineID.DataSource = dt3;
            cbMachineID.DisplayMember = "MachineID";
            cbMachineID.ValueMember = "MachineID";

            //Lấy jigID
            DataTable dt4;
            dt4 = clspartDAO.GetAllJigID();
            DataRow r4 = dt4.NewRow();
            r4["JigType"] = "";
            r4["JigName"] = "";
            dt4.Rows.InsertAt(r4, 0);
            cbJigType.DataSource = dt4;
            cbJigType.DisplayMember = "JigType";
            cbJigType.ValueMember = "JigType";

            //điền giá trị lên các trường
            cbJigType.Text = JigType;
            cbMachineID.SelectedValue = MachineID;
            cbOptionID.SelectedValue = OptionID;
            cbPartID.SelectedValue = PartID;
            cbStaffID.SelectedValue = StaffID;
            txtAondNote.Text = AondNote;
            txtClampTime.Text = ClampTime;
            txtMemo.Text = Memo;
            txtNote.Text = Note;
            txtProTime.Text = ProTime;
            txtToolQty.Text = ToolQty;

            if (CLUpdateday == "")
            {
                dateCLUpdateday.EditValue = null;

            }
            else
            {

                dateCLUpdateday.DateTime = DateTime.Parse(CLUpdateday);
            }
            if (UpdateDay == "")
            {
                dateUpdateDay.EditValue = null;
            }
            else
            {
                dateUpdateDay.DateTime = DateTime.Parse(UpdateDay);
            }

            try
            {
                dateInputDate.DateTime = DateTime.Parse(InputDate);
            }
            catch (Exception)
            {

                dateInputDate.EditValue = null;

            }
            checkLastOption.Checked = bool.Parse(LastOption);
            checkTechDate.Checked = bool.Parse(TechDate);
            checkJig.Checked = bool.Parse(Jig);
            txtJigID.Text = JigID;
        }

        void ThongBao(string name)
        {
            MessageBox.Show("Chưa nhập " + name + ". Vui lòng nhập lại");
        }

        private void btnExit_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnEdit_Click_1(object sender, EventArgs e)
        {
            //if (txtClampTime.Text == "")
            //{
            //    ThongBao(clsConstantsdb.Entities.ClsDBOptionData.FieldName.ClampTime);
            //}
            //else if (txtProTime.Text == "")
            //{
            //    ThongBao(clsConstantsdb.Entities.ClsDBOptionData.FieldName.ProTime);
            //}
            //else if (txtToolQty.Text == "")
            //{
            //    ThongBao(clsConstantsdb.Entities.ClsDBOptionData.FieldName.ToolQty);
            //}
            //else
            //{
            //    if (txtAondNote.Text == "")
            //    {
            //        txtAondNote.Text = "0";
            //    }
            //    try
            //    {
            //        float.Parse(txtAondNote.Text);
            //        float.Parse(txtClampTime.Text);
            //        float.Parse(txtProTime.Text);
            //        int.Parse(txtToolQty.Text);

            //        if (dateCLUpdateday.EditValue == null)
            //        {
            //            dateCLUpdateday.DateTime = DateTime.Parse("1753-01-01 00:00:00.000");
            //        }

            //        if (dateUpdateDay.EditValue == null)
            //        {
            //            dateUpdateDay.DateTime = DateTime.Parse("1753-01-01 00:00:00.000");
            //        }

            //        obj.InputDate = dateInputDate.DateTime;
            //        obj.PartID = cbPartID.SelectedValue.ToString();
            //        obj.MachineID = cbMachineID.SelectedValue.ToString();
            //        obj.OptionID = cbOptionID.SelectedValue.ToString();
            //        obj.JigID = cbJigID.SelectedText;
            //        obj.StaffID = cbStaffID.SelectedValue.ToString();
            //        obj.ToolQty = int.Parse(txtToolQty.Text);
            //        obj.ProTime = float.Parse(txtProTime.Text);
            //        obj.ClampTime = float.Parse(txtClampTime.Text);
            //        obj.TechDate = checkTechDate.Checked;
            //        obj.UpdateDay = dateUpdateDay.DateTime;
            //        obj.Note = txtNote.Text;
            //        obj.AondNote = float.Parse(txtAondNote.Text);
            //        obj.Memo = txtMemo.Text;
            //        obj.CLUpdateday = dateCLUpdateday.DateTime;
            //        obj.LastOption = checkLastOption.Checked;
            //        obj.ID = int.Parse(ID);
            //        obj.Jig = checkJig.Checked;
            //        obj.JigType = txtJigType.Text;

            //        clspartDAO.UpdateOptionData(obj);
            //        MessageBox.Show("Đã nhập xong");
            //        //this.Re_Load(sender, e);
            //    }
            //    catch
            //    {
            //        MessageBox.Show("AondNote, ClampTime, ProTime, ToolQty phải là kiểu số");
            //    }
            //}

            AddData();

        }

        protected override bool ProcessKeyPreview(ref Message m)
        {
            if (m.Msg == 0x0100 && (int)m.WParam == 13)
            {
                this.ProcessTabKey(true);
            }
            return base.ProcessKeyPreview(ref m);
        }

        private void AddData()
        {
            ClsOptionDataTable obj = new ClsOptionDataTable();
            if (GetData(obj))
            {

                //clspartDAO.InsertOptionData(obj);
                if (clspartDAO.UpdateOptionDataNew(obj) > 0)
                {

                    //write Log
                    log.Table = LogConstant.Table.Product.OptionData;
                    log.Task = LogConstant.Task.Update;
                    log.Description = "OptionID: " + obj.OptionID + "; PartID: " + obj.PartID + "; UpdateDay: " + obj.UpdateDay;
                    log.WriteLog();

                    MessageBox.Show("Đã cập nhật xong");

                }
                else
                {
                    Utilities.MessageErrorOk("An error occurred while updating the record to database. Please try again.");
                }
            }
        }

        private bool GetData(ClsOptionDataTable obj)
        {
            #region Check valid

            if ((dateInputDate.EditValue == "1/1/1753" || dateInputDate.EditValue == null || dateInputDate.EditValue.Equals(DateTime.Parse("1/1/0001 12:00:00 AM").ToString("MM/dd/yyyy"))))
            {
                MessageBox.Show("Bạn phải nhập Input Date.");
                dateInputDate.Focus();
                return false;
            }

            if (Utilities.IsNullOrEmpty(cbPartID.SelectedValue) || cbPartID.SelectedItem == cbPartID.Items[0])
            {
                MessageBox.Show("Bạn phải nhập PartID.");
                cbPartID.Focus();
                return false;
            }

            if (Utilities.IsNullOrEmpty(cbMachineID.SelectedValue) || cbMachineID.SelectedItem == cbMachineID.Items[0])
            {
                MessageBox.Show("Bạn phải nhập MachineID.");
                cbMachineID.Focus();
                return false;
            }

            if (Utilities.IsNullOrEmpty(cbOptionID.SelectedValue) || cbOptionID.SelectedItem == cbOptionID.Items[0])
            {
                MessageBox.Show("Bạn phải nhập OptionID.");
                cbOptionID.Focus();
                return false;
            }

            //check dup partID and OptionID

            if (!cbPartID.SelectedValue.ToString().Equals(PartID) || !cbOptionID.SelectedValue.ToString().Equals(OptionID))
            {
                DataTable dtCheckDup =
                BaseDAO.ExecuteQuery("Select * from [242_OptionData] where [PartID] = '" + cbPartID.Text +
                                        "' and [OptionID] = '" + cbOptionID.Text + "'");
                if (dtCheckDup.Rows.Count > 0)
                {
                    MessageBox.Show("Đã có PartNo và OptionID này trong dữ liệu.");
                    cbPartID.Focus();
                    return false;
                }
            }



            //if (!Utilities.IsNullOrEmpty(cbJigID.Text) && (Utilities.IsNullOrEmpty(cbJigID.SelectedValue) || cbJigID.SelectedItem == cbJigID.Items[0]))
            //{
            //    MessageBox.Show("Jig không hợp lệ.");
            //    cbJigID.Focus();
            //    return false;
            //}

            //if (Utilities.IsNullOrEmpty(cbStaffID.SelectedValue) || cbStaffID.SelectedItem == cbStaffID.Items[0])
            //{
            //    MessageBox.Show("StaffID không hợp lệ.");
            //    cbStaffID.Focus();
            //    return false;
            //}

            if (!txtJigID.Text.Trim().Equals(string.Empty) && Utilities.IsNullOrEmpty(cbJigType.Text.Trim()))
            {
                MessageBox.Show("Bạn phải chọn Jig Type.");
                cbJigType.Focus();
                return false;
            }


            if (Utilities.IsNullOrEmpty(txtToolQty.Text.Trim()))
            {
                //MessageBox.Show("Bạn phải nhập Tool Qty.");
                txtToolQty.Text = "0";
                //txtToolQty.Focus();
               // return false;
            }

            if (Utilities.IsNullOrEmpty(txtProTime.Text.Trim()))
            {
                MessageBox.Show("Bạn phải nhập ProTime.");
                txtProTime.Focus();
                return false;
            }

            if (Utilities.IsNullOrEmpty(txtClampTime.Text.Trim()))
            {
                MessageBox.Show("Bạn phải nhập ClampTime.");
                txtClampTime.Focus();
                return false;
            }

            if (Utilities.IsNullOrEmpty(txtAondNote.Text.Trim()))
            {
                txtAondNote.Text = "0";
                txtAondNote.Focus();
                //return false;
            }


            try
            {
                float.Parse(txtProTime.Text);
                float.Parse(txtClampTime.Text);
                int.Parse(txtToolQty.Text);
            }
            catch (Exception)
            {
                MessageBox.Show("ToolQty, ProTime, ClampTime phải là số.");
                txtProTime.Focus();
                return false;
            }

            if (float.Parse(txtToolQty.Text) < 0 || float.Parse(txtProTime.Text) < 0 || float.Parse(txtClampTime.Text) < 0)
            {
                MessageBox.Show("ToolQty, ProTime, ClampTime phải lớn hơn hoặc bằng 0.");
                txtProTime.Focus();
                return false;

            }


            #endregion

            //obj.InputDate = dateInputDate.DateTime;
            obj.InputDate = (dateInputDate.EditValue == "1/1/1753" || dateInputDate.EditValue == null || dateInputDate.EditValue.Equals(DateTime.Parse("1/1/0001 12:00:00 AM").ToString("MM/dd/yyyy"))) ? DateTime.Parse(minDate) : dateInputDate.DateTime;
            obj.ID = int.Parse(ID);
            obj.PartID = cbPartID.Text;
            obj.MachineID = cbMachineID.Text;
            obj.OptionID = cbOptionID.Text;
            obj.JigID = txtJigID.Text;
            obj.StaffID = (Utilities.IsNullOrEmpty(cbStaffID.SelectedValue) || cbStaffID.SelectedItem == cbStaffID.Items[0])? string.Empty: cbStaffID.SelectedValue.ToString();
            obj.ToolQty = int.Parse(txtToolQty.Text);
            obj.ProTime = float.Parse(txtProTime.Text);
            obj.ClampTime = float.Parse(txtClampTime.Text);
            obj.TechDate = checkTechDate.Checked;

            obj.UpdateDay = (dateUpdateDay.EditValue == "1/1/1753" || dateUpdateDay.EditValue == null || dateUpdateDay.EditValue.Equals(DateTime.Parse("1/1/0001 12:00:00 AM").ToString("MM/dd/yyyy"))) ? DateTime.Parse(minDate) : dateUpdateDay.DateTime;
            obj.CLUpdateday = (dateCLUpdateday.EditValue == "1/1/1753" || dateCLUpdateday.EditValue == null || dateCLUpdateday.EditValue.Equals(DateTime.Parse("1/1/0001 12:00:00 AM").ToString("MM/dd/yyyy"))) ? DateTime.Parse(minDate) : dateCLUpdateday.DateTime;

            obj.Note = txtNote.Text;
            obj.AondNote = txtAondNote.Text;
            obj.Memo = txtMemo.Text;
            obj.LastOption = checkLastOption.Checked;
            obj.Jig = checkJig.Checked;
            obj.JigType = cbJigType.Text;

            return true;
        }

        private void Frm_OptionDataEdit_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Alt)
            {
                if (e.KeyCode == Keys.D)
                {
                    this.btnEdit.PerformClick();
                }
            }
            else if (e.KeyCode == Keys.End)
            {
                btnEdit.Focus();
            }
            else if (e.KeyCode == Keys.Home)
            {
                cbPartID.Focus();
            }
        }

    }
}